package cn.biplam.front.page;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.view.mainpage.MessageAudit;
import cn.biplam.front.component.view.mainpage.MessageErr;
import cn.biplam.front.component.view.mainpage.MessageJob;
import cn.biplam.front.component.view.mainpage.MessageMonitor;
import cn.biplam.front.component.view.mainpage.MessageMsg;
import cn.biplam.front.component.view.mainpage.UserCall;
import cn.biplam.front.component.view.systemManage.menu.SystemMenu;
import cn.biplam.front.page.mainpage.BannerButtons;
import cn.biplam.front.template.main.PageMain;

public class Main {
	private HttpServletRequest request=null;
	private HttpServletResponse response=null;
	private PageMain page;
	String sHomeUrl;
	
	/**
	 * 构造方法
	 * @param request
	 * @param response
	 */
	public Main(HttpServletRequest request, HttpServletResponse response){
		this.request=request;
		this.response=response;
	}
	
	/**
	 * 返回页面的HTML
	 */
	public void draw() throws ErrorMessage{
		UserInfo mUser = LoginDo.isLogin(request, response);
		//用户是否登录的判断，如果登陆将从session里获取用户信息类，否则跳转到登陆页面。
		if(mUser == null) return;
		
		try {
			this.response.setContentType("text/html"); 
			PrintWriter out = new PrintWriter (this.response.getOutputStream());
			
			this.sHomeUrl=request.getContextPath();
			this.page=new PageMain("主界面", sHomeUrl,mUser);
			
			/**放入Banner右边的按钮组*/
			page.putButtons(BannerButtons.get(mUser,sHomeUrl));
			/**放入用户称呼*/
			page.putUserCall(new UserCall().get(mUser, sHomeUrl));
			/**放入用户ID*/
			page.setUserID(mUser.getUserID());
			
			/**放入消息提示*/
			page.putMessage(MessageMsg.get(mUser, sHomeUrl));
			page.putMessage(MessageAudit.get(mUser, sHomeUrl));
			page.putMessage(MessageJob.get(mUser, sHomeUrl));
			page.putMessage(MessageMonitor.get(mUser, sHomeUrl));
			page.putMessage(MessageErr.get(mUser, sHomeUrl));
			
			/**放入左部导航*/
			page.putMenu(new SystemMenu().get(mUser,this.sHomeUrl));
			
			/**放入右部功能区*/
			
			out.println(page.get().draw());

			out.close();
		} catch(ErrorMessage e){
			throw e;
		} catch (Exception e) {
			throw new ErrorMessage(e);
		}
	}

}
